package com.house.java.action;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.house.java.action.newhouse.NewHouseAction;
import com.house.java.action.news.NewsAction;
import com.house.java.action.secondhouse.SecondHouseAction;
import com.house.java.action.user.UserAction;

public class QueryHouseAction extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(QueryHouseAction.class);

	public void service(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		// 获取请求路径
		String uri = request.getRequestURI();
		// 从 web.xml文件中读取初始化参数 suffix
		ServletConfig config = this.getServletConfig();

		String suffix = config.getInitParameter("suffix");

		// 对uri进行截取 ems_v2/list.do ---> 截取出 list
		// substring(m,n) ----> 截取 从m开始 到n-1处结束。不包括n。一共是n-m个字符
		String path = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf(suffix));
//		String path = uri.substring(uri.lastIndexOf("/") + 1);
		String lastPath = "";
		if(path.equalsIgnoreCase("newhouse")){
			try{
				NewHouseAction action = new NewHouseAction();
				action.queryNewHouseByCondition(request, response);
				lastPath = "/newhouse_list.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if(path.equalsIgnoreCase("newHouseDetail")){
			try{
				NewHouseAction action = new NewHouseAction();
				action.queryById(request, response);
				lastPath = "/newhouse_detail.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if (path.equalsIgnoreCase("secondhouse")) {
			try{
				SecondHouseAction action = new SecondHouseAction();
				action.querySecondHouseByCondition(request, response);
				lastPath = "/secondhouse_list.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if (path.equalsIgnoreCase("secondhousedetail")) {
			try{
				SecondHouseAction action = new SecondHouseAction();
				action.querySecondHouseById(request, response);
				lastPath = "/secondhouse_detail.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if (path.equalsIgnoreCase("news")) {
			try{
				NewsAction action = new NewsAction();
				action.queryNewsByCondition(request, response);
				lastPath = "/news_list.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if (path.equalsIgnoreCase("newsdetail")) {
			try{
				NewsAction action = new NewsAction();
				action.queryNewsByCondition(request, response);
				lastPath = "/news_detail.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if (path.equalsIgnoreCase("userlist")) {
			try{
				UserAction action = new UserAction();
				action.queryUserByCondition(request, response);
				lastPath = "/user_list.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}else if (path.equalsIgnoreCase("userdetail")) {
			try{
				UserAction action = new UserAction();
				action.queryUserById(request, response);
				lastPath = "/user_detail.jsp";
			}catch (Exception e) {
				log.error(e.getMessage());
				response.sendRedirect("error.jsp");
				return ;
			}
		}
		request.getRequestDispatcher(lastPath).forward(request, response);
		
	}

}
